package com.hf.wujian.dispatch.imp;

import com.hf.wujian.annotation.Dispatch;
import com.hf.wujian.constant.Command;
import com.hf.wujian.dispatch.BSHDispatch;
import com.hf.wujian.model.GameHall;
import com.hf.wujian.model.GameRoom;
import com.hf.wujian.model.GameUser;
import com.hf.wujian.netty.NettyMsg;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 加入房间调度类
 *
 * @author hf
 * @date 2018/11/23
 * @version 1.0
 *
 */
@Dispatch(command = Command.COMMAND_JOINGAMEROOM)
public class JoinRoomDispatch implements BSHDispatch {
    @Override
    public void dispatch(ChannelHandlerContext ctx, NettyMsg nettyMsg, GameHall gameHall, GameUser gameUser) {
//        //随机获取一个房间
//        List<String> rooms = gameHall.getGameRooms();
//        Random random = new Random();
//        if(rooms.size()>0){
//            String roomName = rooms.get(random.nextInt(rooms.size()));
//            Map<String,Object> data = nettyMsg.getData();
//            GameRoom gameRoom = gameHall.getGameRoom(roomName);
//            if(gameRoom != null){
//                gameRoom.addGameUser(gameUser);
//            }
//        }
        String roomName = (String) nettyMsg.getData().get("roomName");
        GameRoom gameRoom = gameHall.getGameRoom(roomName);
        if(gameRoom != null){
            gameRoom.addGameUser(gameUser);
            gameUser.joinRoom(gameRoom);
        }
    }
}
